#include<bits/stdc++.h>

using namespace std;

string path , SSS;
struct TreeNode 
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

TreeNode* dfs_d(int& u)
{
    if(u > SSS.size()) return NULL;
    if(u == '#')
    {
        u += 2;
        return NULL;
    }
    int k = u;
    while(SSS[k] != ',') ++k;
    int val = stoi(SSS.substr(u , k - u));
    TreeNode* r = new TreeNode(val);
    //跳过逗号
    u = k + 1;
    r->left = dfs_d(u);
    r->right = dfs_d(u);
    return r;
}
TreeNode* deserialize(string data , int& u) 
{
    // return NULL;
    SSS = data;
    return dfs_d(u);
}
TreeNode* deserialize(int& u) 
{
    // return NULL;
    // SSS = data;
    if(SSS.size() == 0)
    {
        cout<<"error"<<endl;
        return NULL;
    } 
    return dfs_d(u);
}

void dfs_s(TreeNode* r)
{
    if(!r)
    {
        path += "#,";
        return ;
    }
    else
    {
        int val = r->val;
        path += to_string(val) + ",";
        dfs_s(r->left);
        dfs_s(r->right); 
    }
}
string serialize(TreeNode* root) 
{
    dfs_s(root);
    cout<<path<<endl;
    return path;
}
void dfs_pre(TreeNode* r)
{
    if(!r) return;
    cout<<r->val<<" ";
    dfs_pre(r->left);
    dfs_pre(r->right);
}

int main()
{
    SSS = "1,2,4,#,#,5,#,#,3,6,#,#,7,#,#,";
    int u = 0;
    TreeNode* r = deserialize(u);
    dfs_pre(r);
    // cout<<serialize(r)<<endl;
    system("pause");
    return 0;
}